package com.muchenxinxi.androidbaseapp.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;

/**
 * ┏-----------------------------------------┓
 * │                  /) /)                  │
 * │               ฅ(• ﻌ •)ฅ                 │
 * │ Created by HaoKing on 2017/04/27 14:15. │
 * ┣----┳-----------------------------------┛
 * │Note│ 只能输入两位小数的输入框
 * ┣----┻-----------------------------------┓
 * │           www.muchenxinxi.com           │
 * │            www.nextdever.com            │
 * │            QQ: 773693552                │
 * │      QQ Group: Geek Chat 344386592      │
 * ┗-----------------------------------------┛
 */

public class EditMoneyText extends AppCompatEditText {
    public EditMoneyText(Context context) {
        super(context);
        initEditMoneyText();
    }

    public EditMoneyText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initEditMoneyText();
    }

    public EditMoneyText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initEditMoneyText();
    }

    private void initEditMoneyText() {
        this.setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
        this.setKeyListener(DigitsKeyListener.getInstance(false, true));
        this.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                /**
                 * 限制输入金额最多为 limit 位小数
                 */
                int limit = 2;
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > limit) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + limit + 1);
                        EditMoneyText.this.setText(s);
                        EditMoneyText.this.setSelection(s.length());
                    }
                }
                /**
                 * 第一位输入小数点的话自动变换为 0.
                 */
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    EditMoneyText.this.setText(s);
                    EditMoneyText.this.setSelection(2);
                }

                /**
                 * 避免重复输入小数点前的0 ,没有意义
                 */
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        EditMoneyText.this.setText(s.subSequence(0, 1));
                        EditMoneyText.this.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
}
